package cn.jn.lhm.vod.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.jn.lhm.service.config.exception.GlobalException;
import cn.jn.lhm.vod.service.VodService;
import cn.jn.lhm.vod.utils.VodConstant;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/7 - 21:16
 */
@Service
@Slf4j
public class VodServiceImpl implements VodService {


    /**
     * fileName：文件的原始名称
     * title：文件上传后的显示名称
     * inputStream：文件流
     * @param file 文件流
     * @return
     */
    @Override
    public String uploadVideoToAliyun(MultipartFile file) {
        String filename = file.getOriginalFilename();
        //去除视频的后缀名
        String title = filename.substring(0, filename.lastIndexOf("."));
        UploadStreamRequest request = null;
        try {
            request = new UploadStreamRequest(VodConstant.ACCESS_KEY_ID, VodConstant.ACCESS_KEY_SECRET
                    , title, filename, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        return  response.getVideoId();
    }

    @Override
    public void deleteAliyunVodById(String id) {
        try {
            DefaultAcsClient client = initVodClient(VodConstant.ACCESS_KEY_ID, VodConstant.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            log.info("开始单条删除阿里云中的数据，时间为：" + new Date() + "\t 数据为：" + id);
            request.setVideoIds(id);
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GlobalException(20001,"删除视频失败");
        }
    }

    @Override
    public void removeVideoBatch(List<String> videoIdList) {
        try {
            DefaultAcsClient client = initVodClient(VodConstant.ACCESS_KEY_ID, VodConstant.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            if (!StringUtils.isEmpty(videoIdList)){
                String videoIdStr = CollUtil.join(videoIdList, ",");
                log.info("开始批量删除阿里云中的数据，时间为：" + new Date() + "\t 数据为：" + videoIdStr);
                request.setVideoIds(videoIdStr);
                client.getAcsResponse(request);
            }
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GlobalException(20001,"删除视频失败");
        }
    }

    @Override
    public String getPlayAuth(String id) {
        try {
            DefaultAcsClient client = initVodClient(VodConstant.ACCESS_KEY_ID, VodConstant.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            return response.getPlayAuth();
        } catch (Exception e){
            e.printStackTrace();
            throw new GlobalException(20001,"获取视频信息失败");
        }
    }

    /**
     * 阿里云媒体管理初始化
     * @param accessKeyId oss key
     * @param accessKeySecret oss
     * @return
     * @throws ClientException
     */
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
        // 点播服务接入区域
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
}
